//
//  firendViewController.m
//  tcpoctest
//
//  Created by gump on 10/22/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "firendViewController.h"
#import "WFLabel_blackShadow.h"
#import "PersonCell.h"
#import "ViewManager.h"
#import "PublicFun.h"
#import "FirendInfo.h"
#import "CareDataSource.h"

@interface firendViewController ()

@end

@implementation firendViewController

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"朋友";
    
    self.segmentedControl = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, TOPBAR_HIGHT, 320, 44)];
    [self.segmentedControl setSectionTitles:@[@"我关注谁", @"谁关注我"]];
    [self.segmentedControl setSelectedSegmentIndex:0];
    [self.segmentedControl setBackgroundColor:[UIColor colorWithRed:68/255.0 green:68/255.0 blue:68/255.0 alpha:1]];
    [self.segmentedControl setTextColor:[UIColor whiteColor]];
    [self.segmentedControl setSelectionIndicatorColor:
     [UIColor colorWithRed:77/255.0 green:131/255.0 blue:220/255.0 alpha:1]];
    
    __block firendViewController *blockself = self;
    
    [self.segmentedControl setIndexChangeBlock:^(NSInteger index) {
        [blockself.scrollView scrollRectToVisible:CGRectMake(320 * index, 0, 320, 200) animated:YES];
    }];
    [self.segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedControl];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                                                                     TOPBAR_HIGHT + 44,
                                                                     WINDOW_WIDTH,
                                                                     WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)];
    [self.scrollView setBackgroundColor:[UIColor colorWithRed:0.1 green:0.2 blue:0.34 alpha:1]];
    [self.scrollView setPagingEnabled:NO];
    self.scrollView.scrollEnabled = NO;
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    [self.scrollView setContentSize:CGSizeMake(WINDOW_WIDTH * 2, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44)];
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, 320, WINDOW_HIGTH - TOPBAR_HIGHT - 20 - 44) animated:NO];
    [self.scrollView setDelegate:self];
    [self.view addSubview:self.scrollView];
    
    myInterestTablev =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 64 - 44)
                                         style:UITableViewStylePlain];
    myInterestTablev.backgroundColor = CELL_BG_COLOR;
    myInterestTablev.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    myInterestTablev.delegate = self;
    myInterestTablev.dataSource = self;
    [self.scrollView addSubview:myInterestTablev];
    
    InterestMeTablev =[[UITableView alloc]initWithFrame:CGRectMake(WINDOW_WIDTH, 0, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 64 - 44)
                                                  style:UITableViewStylePlain];
    InterestMeTablev.backgroundColor = CELL_BG_COLOR;
    InterestMeTablev.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    InterestMeTablev.delegate = self;
    InterestMeTablev.dataSource = self;
    [self.scrollView addSubview:InterestMeTablev];
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"找朋友" target:self action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
}

#pragma mark -private fun-
-(void)nextButtonPressed
{
    [[ViewManager sharedViewManager] showView:VI_SEARCHFIREND
                             withIsRemoveCurV:true
     withDirection:VD_RIGHT];
}

-(void)reloadFirendView
{
    [myInterestTablev reloadData];
    [InterestMeTablev reloadData];
}

-(void)refreshView
{
    [myInterestTablev reloadData];
    [InterestMeTablev reloadData];
}

-(void)viewWillShow
{
    [myInterestTablev reloadData];
    [InterestMeTablev reloadData];
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
	
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    CGFloat pageWidth = scrollView.frame.size.width;
//    NSInteger page = scrollView.contentOffset.x / pageWidth;
//    [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == myInterestTablev) {
        return [[DataControl shareControl].careds GetShowArrCount];
    }
    else if(tableView == InterestMeTablev){
        return [[DataControl shareControl].becareds GetShowArrCount];
    }
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    NSUInteger row = [indexPath row];
    
    static NSString *CellIdentifier = @"Cell";
    PersonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[PersonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSString *strmail;//帐户名称
    FirendInfo  *fi;//朋友信息
    NSString *url;//朋友图像
    
    if (tableView == myInterestTablev) {//我关注的人
        strmail = [[DataControl shareControl].careds GetShowArrObjectByIndex:row];
        fi = [[DataControl shareControl] SearchInfo: strmail];
        url = [[DataControl shareControl] SearchHeadImage:strmail];
    }
    else if(tableView == InterestMeTablev){//关注我的人
        strmail = [[DataControl shareControl].becareds GetShowArrObjectByIndex:row];
        fi = [[DataControl shareControl] SearchInfo: strmail];
        url = [[DataControl shareControl] SearchHeadImage:strmail];
    }
    
    switch (section)
    {
        case 0:
        {
            if (fi != nil)
            {
                cell.nameLabel.text = fi.name;
                cell.signLabel.text = fi.strSign;
                cell.ageLabel.text = [NSString stringWithFormat:@"%d",fi.age];
                if (fi.sex == 1) {
                    cell.sexicon.image = [UIImage imageNamed:@"icon_male.png"];
                }else{
                    cell.sexicon.image = [UIImage imageNamed:@"icon_female.png"];
                }
                if ([url length] > 0)
                {
                    cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                    UIImage *headImage = [UIImage imageWithContentsOfFile:url];
                    if (headImage) {
                        cell.imageView.image = nil;
                        cell.headImageView.image = [UIImage imageWithContentsOfFile:url];
                    }
                }else{
                    cell.headImageView.image = nil;
                    cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
                }
            }else{
                cell.imageView.image = [UIImage imageNamed:@"default_head.png"];
            }
            cell.disLabel.text = nil;
            
        }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            break;
        default:
            break;
    }
    
    [cell changeCellSize];//动态控制控件的坐标
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *strmail;
    if (tableView == myInterestTablev) {
        strmail = [[DataControl shareControl].careds GetShowArrObjectByIndex:[indexPath row]];
    }
    else if(tableView == InterestMeTablev){
        strmail = [[DataControl shareControl].becareds GetShowArrObjectByIndex:[indexPath row]];
    }
    
    switch ([indexPath section]) {
        case 0:
        {
            [DataControl shareControl].currentFI = [[DataControl shareControl] SearchInfo:strmail];
            
            [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                     withIsRemoveCurV:false withDirection:VD_RIGHT];
        }
            break;
            
        default:
            break;
    }
}

@end
